<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    

<script>

// find()返回数组中第一个符合条件的元素
let arr1 = [78,95,46,67,100,58];
let res1 = arr1.find((item,index,arr)=>{
    return item < 60;
});
let res2 = arr1.find(item=>item < 60);
let res3 = arr1.find(item=>item > 100);
console.log( res1 );// 46
console.log( res2 );// 46
console.log( res3 );// undefined

// findIndex()返回数组中第一个符合条件的元素的下标，没有返回-1
let arr2 = [
    {name:'小明',age: 24},
    {name:'小红',age: 17},
    {name:'小芳',age: 22},
    {name:'小东',age: 15},
    {name:'小王',age: 25}
];

// console.log( arr2.indexOf({name:'小红',age: 17}) );// -1

// for (var i = 0, len = arr2.length; i < len; i++) {
//     if (arr2[i].age < 18) {
//         console.log( i );// 1 3
//     }
// }

// for (var i = 0; arr2[i]; i++) {
//     arr2[i].age < 18 && console.log( i );// 1 3
// }

let res4 = arr2.findIndex(val => val.age < 18);
console.log( res4 );// 1

let res5 = arr2.findIndex((val,index,arr)=>{
    return val.age < 18;
});
console.log( res5 );// 1

</script>
</body>
</html>